package network;

import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;

import link.LinkInterface;

public class RoutingTable {
	/**
	 * The actual table that contains TPA adresses as keys and LinkInterfaces as
	 * values.
	 */
	Map<Integer, LinkInterface> entrytable;
	
	DatagramSocket socket;

	/**
	 * Creates a new RoutingTable which contains an entrytable.
	 */
	public RoutingTable() {
		entrytable = new HashMap<Integer, LinkInterface>();
	}

	/**
	 * Enters an adress in the table. Should an adress already be in the table,
	 * an exception will be thrown with the message that the adress already is
	 * stored.
	 * @param adress the TPA adress.
	 * @param outgoing the LinkInterface, which determines an IP-adress or the LTP.
	 * @ensures entrytable.containsKey(adress);
	 */
	public void enterAdress(TPA adress, LinkInterface outgoing) {
		if (!entrytable.containsKey(adress)) {
			entrytable.put(adress.getAdress(), outgoing);
		} else {
			throw new IllegalArgumentException(
					"Adress is already in the routing table.");
		}

	}

	/**
	 * Returns the corresponding LinkInterface corresponding to the TPA
	 * @param adress the TPA of which the LinkInterface is requested
	 * @requires entrytable.contains(adress)
	 * @return the corresponding LinkInterface belonging to the TPA
	 */
	public LinkInterface rout(TPA adress) {
		LinkInterface ret = entrytable.get(adress.getAdress());
		
		return ret;

	}

}
